package com.mokee.widget.TaiJiTu;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class TaiJiTu extends View {
	public static final String tag = "TaiJiTu";

	private Paint whitePaint;
	private Paint blackPaint;
	
	private float degrees = 0;// 旋转角度
	private boolean isAutoRotate = false;
	private int autoRotateDegrees = 2;
	private int autoRotateSpeed = 20;
	private int padding = 50;
	private int background = Color.TRANSPARENT;
	
	/**
	 * 初始化画笔
	 */
	private void initPaints() {
		whitePaint = new Paint();
		whitePaint.setAntiAlias(true);
		whitePaint.setColor(Color.WHITE);
		
		blackPaint = new Paint();
		blackPaint.setAntiAlias(true);
		blackPaint.setColor(Color.BLACK);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		/*****************确定画布的中心点*****************/
		int width = canvas.getWidth();
		int height = canvas.getHeight();
		Point centerPoint = new Point(width / 2, height / 2);
		
		/**********将Canvas坐标原点移动至视图中心***********/
		canvas.translate(centerPoint.x, centerPoint.y);
		
		/*******************绘制背景色*********************/
		canvas.drawColor(background);
		canvas.rotate(degrees);
		if(isAutoRotate) {
			degrees += autoRotateDegrees;
			if (degrees > 360) {
				degrees = 0;
			}
			postInvalidateDelayed(autoRotateSpeed);
		}
		
		/*********确定太极的半径大小并绘制两个半圆**********/
		int radius = Math.min(width, height) / 2 - padding;// 太极半径
		RectF rect = new RectF(-radius, -radius, radius, radius);// 绘制区域
		canvas.drawArc(rect, 90, 180, true, blackPaint);// 绘制黑色半圆
		canvas.drawArc(rect, -90, 180, true, whitePaint);// 绘制白色半圆
		
		/******************绘制两个小圆 *******************/
		int smallRadius = radius / 2;
		canvas.drawCircle(0, -smallRadius, smallRadius, blackPaint);
		canvas.drawCircle(0, smallRadius, smallRadius, whitePaint);
		
		/********************绘制鱼眼*********************/
		canvas.drawCircle(0, -smallRadius, smallRadius / 4, whitePaint); 
		canvas.drawCircle(0, smallRadius, smallRadius / 4, blackPaint);
	}
	
	/**
	 * 设置旋转角度
	 * @param degrees
	 */
	public void setRotate(float degrees) {
		this.degrees = degrees;
		invalidate();
	}
	
	/**
	 * 设置是否自动旋转
	 * @param autoRotate	是否自动旋转
	 * @param autoRotateDegrees 自动旋转角度
	 * @param autoRotateSpeed 自动旋转速度
	 */
	public void setAutoRotate(boolean autoRotate, int autoRotateDegrees, int autoRotateSpeed) {
		this.isAutoRotate = autoRotate;
		if(autoRotate) {
			this.autoRotateDegrees = autoRotateDegrees;  
			this.autoRotateSpeed  = autoRotateSpeed;
		}
		invalidate();
	}
	
	/**
	 * 设置太极图和试图View的四周的距离，默认50
	 * @param padding
	 */
	public void setPadding(int padding) {
		this.padding = padding;
		invalidate();
	}
	
	/**
	 * 设置View的背景色，默认为透明：Color.TRANSPARENT
	 * @param color
	 */
	public void setViewBackground(int color) {
		this.background =  color;
		invalidate();
	}
	
	public TaiJiTu(Context context, AttributeSet attrs) {
		super(context, attrs);
		initPaints();
	}
	
	public TaiJiTu(Context context) {
		super(context);
		initPaints();
	}
}
